/**
 * app angularjs moudle
 * 整个页面为一个大的moudle
 * 
 * @authors YangSh
 * @date    2017-4-19
 * @version  0.0.1
 */
define([
    'angular','service','uiRouter','ngMessages','ngTable','ngAnimate','uiBootstrapTpls',
        'directives/utilDirective','filter/utilFilter'
    , 'ngFileUpload'
    ], function (angular) {
        'use strict';
        var app = angular.module ('books', ['books.service','ui.router','ngMessages','ngTable','ngAnimate','ui.bootstrap',
            'directiveModule','filterModule'
            , 'ngFileUpload']);

        app.factory('localStorageService',['$window',function ($window) {
            return {
                setToken:function(value){
                  $window.localStorage['token'] = value;
                },        //读取对象
                getToken: function () {
                  if(typeof($window.localStorage['token']) == "undefined"){
                     return "";
                  }else{
                     return $window.localStorage['token'];
                  }
                },
                setName:function(value){
                  $window.localStorage['name'] = value;
                },        //读取对象
                getName: function () {
                  if(typeof($window.localStorage['name']) == "undefined" || $window.localStorage['name'] == "undefined"){
                     return "";
                  }else{
                     return $window.localStorage['name'];
                  }
                 
                }
            }
        }]);
        app.config(function($httpProvider){
            var interceptor = function($q,$rootScope,localStorageService){
                return {
                    'request' :function(req){//请求
                        var token = localStorageService.getToken();
                        if(token =="" || token == undefined){
                            return req;
                        }
                        req.params = req.params || {};
                        if (!req.params.token) {
                            //TODO:每个方法添加一个token参数
                             //req.params.token = localStorageService.getToken();
                        }
                        return req;
                    },
                    'requestError' :function(req){//请求失败
                        return req;
                    },
                    'response' :function(resp){//请求
                        //登录成功后 添加要保存的信息
                        // if(resp.config.url = "http://47.92.95.246:8012/login"){
                        //     localStorageService.setName(resp.data.name);
                        //     console.log(resp);
                        //     localStorageService.setToken(resp.data.token);
                        // }
                        return resp;
                    },
                    'responseError' :function(resp){//响应
                        // switch(resp.status){
                        //     case '401':
                        //         alert("401 error!");
                        //         break;
                        //     case '404':
                        //         alert("404 error!");
                        //         break;
                        //     case '500':
                        //         alert("500 server error!");
                        //         break;
                        // }
                        return $q.reject(resp);
                    }
                }
            };
            $httpProvider.interceptors.push(interceptor);//将拦截器添加到头中
        });

        return app;
    }
);